package hlhj.fhp.supreme.js

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.webkit.JavascriptInterface
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.convert.StringConvert
import com.lzy.okgo.model.Response
import com.lzy.okrx.adapter.ObservableBody
import fhp.hlhj.giantfold.utils.LogUtil
import hlhj.fhp.supreme.activitys.ConfirmOrderAty
import hlhj.fhp.supreme.javabean.BaseBean
import hlhj.fhp.supreme.javabean.CartBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.JsonConvert
import hlhj.fhp.supreme.utils.MyUtils
import java.util.ArrayList

/**
 * Created by Administrator on 2018\4\9 0009.
 */
class JsHelper(private val context: Activity,private val goodId:String,private val webView: android.webkit.WebView,private val token:String) {
    /**
     * 返回
     */
    @JavascriptInterface
    fun finishAty(){
        val activity = context as Activity
        activity.finish()
    }
    /*
    * 立即购买
    * */
    @JavascriptInterface
    fun buyNow(goods_id:String,number:String,money:String,img :String,tittle:String,score:String){
//        MyUtils.toast("立即购买$goods_id----$number-----${goods_id.toInt()}")

        val list =ArrayList<CartBean.DataBean>()
        val bean = CartBean.DataBean()
        bean.money=money
        bean.title=tittle
        bean.id=goods_id.toInt()
        bean.img=img
        bean.score=score
        bean.number=1
        list.add(bean)
        LogUtil.logi("商品钱${money}积分${score}")
        val intent = Intent(context, ConfirmOrderAty::class.java)
        intent.putExtra("et","1")
        intent.putExtra("datas",list)
        intent.putExtra("totalMoney",if (money==null||money.equals("0")) "${bean.score}积分" else "￥${bean.money}")
        intent.putExtra("type",if (money==null||money.equals("0"))"1" else "0")
        context.startActivity(intent)
        context.finish()
    }

    /**
     * 添加购物车
     */
    @JavascriptInterface
    fun addCart(id:String,number:String){
        MyUtils.toast("加入购物车")
        OkGo.post<BaseBean>(Urls.BaseUrl+Urls.ADD_CART)
                .params("id",goodId)
                .params("number",1)
                .execute(object :JsonCallBack<BaseBean>(context){
                    override fun onSuccess(response: Response<BaseBean>?) {
                        val bean = response?.body()
                        if (bean?.code==1){
                            MyUtils.toast(bean?.msg)
                        }
                    }
                })
    }

    /**
     * 获取商品id
     */
    fun getGoodId(): String {
        return goodId
    }

    @JavascriptInterface
    /**
     * 获取商品详情
     */
    fun getGoodInfo(){
        OkGo.post<String>(Urls.BaseUrl+Urls.GOOD_DETAIL)
                .headers("token",token)
                .params("id",goodId)
                .execute(object :StringCallback(){
                    override fun onSuccess(response: Response<String>?) {
                        val str = response?.body()
                        webView.loadUrl("javascript:getData($str)")
                    }
                })
    }



}